package com.vegetable.modules.service.finance.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vegetable.modules.entity.finance.po.CustomerCollectionAlertRecordQueryPO;
import com.vegetable.modules.entity.finance.vo.CustomerCollectionAlertRecordVO;
import com.vegetable.modules.entity.finance.vo.CustomerOverdueStatisticsVO;
import com.vegetable.modules.entity.finance.vo.OverdueLevelStatisticsVO;
import com.vegetable.modules.mapper.finance.CustomerCollectionAlertRecordMapper;
import com.vegetable.modules.service.finance.ICustomerCollectionAlertRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 客户回款预警 - 实时查询服务实现类
 * </p>
 *
 * @author vegetable
 * @since 2025-10-24
 */
@Slf4j
@Service
public class CustomerCollectionAlertRecordServiceImpl implements ICustomerCollectionAlertRecordService {

    @Autowired
    private CustomerCollectionAlertRecordMapper baseMapper;

    @Override
    public IPage<CustomerCollectionAlertRecordVO> queryOverdueOrders(CustomerCollectionAlertRecordQueryPO queryPO) {
        // 将PO对象转换为Map参数
        Map<String, Object> params = new HashMap<>();
        params.put("customerId", queryPO.getCustomerId());
        params.put("customerName", queryPO.getCustomerName());
        params.put("docNo", queryPO.getDocNo());
        params.put("alertLevel", queryPO.getAlertLevel());
        params.put("overdueLevel", queryPO.getOverdueLevel());
        params.put("startTime", queryPO.getStartTime());
        params.put("endTime", queryPO.getEndTime());
        params.put("regionId", queryPO.getRegionId());
        
        // 计算分页参数
        long current = queryPO.getPage();
        long size = queryPO.getLimit();
        long offset = (current - 1) * size;
        params.put("offset", offset);
        params.put("limit", size);
        
        // 查询总数
        Long total = baseMapper.countOverdueOrders(params);
        
        // 查询列表数据
        List<CustomerCollectionAlertRecordVO> records = baseMapper.selectOverdueOrdersList(params);
        
        // 手动构建分页结果
        Page<CustomerCollectionAlertRecordVO> page = new Page<>(current, size, total);
        page.setRecords(records);
        
        return page;
    }

    @Override
    public List<OverdueLevelStatisticsVO> queryOverdueLevelStatistics(CustomerCollectionAlertRecordQueryPO queryPO) {
        // 将PO对象转换为Map参数
        Map<String, Object> params = new HashMap<>();
        params.put("customerId", queryPO.getCustomerId());
        params.put("customerName", queryPO.getCustomerName());
        params.put("startTime", queryPO.getStartTime());
        params.put("endTime", queryPO.getEndTime());
        params.put("regionId", queryPO.getRegionId());
        
        // 查询统计数据
        return baseMapper.selectOverdueLevelStatistics(params);
    }

    @Override
    public IPage<CustomerCollectionAlertRecordVO> queryOverdueLevelDetail(CustomerCollectionAlertRecordQueryPO queryPO) {
        // 将PO对象转换为Map参数
        Map<String, Object> params = new HashMap<>();
        params.put("customerId", queryPO.getCustomerId());
        params.put("customerName", queryPO.getCustomerName());
        params.put("startTime", queryPO.getStartTime());
        params.put("endTime", queryPO.getEndTime());
        params.put("regionId", queryPO.getRegionId());
        params.put("overdueLevel", queryPO.getOverdueLevel());
        
        // 计算分页参数
        long current = queryPO.getPage();
        long size = queryPO.getLimit();
        long offset = (current - 1) * size;
        params.put("offset", offset);
        params.put("limit", size);
        
        // 查询总数
        Long total = baseMapper.countOverdueLevelDetail(params);
        
        // 查询列表数据
        List<CustomerCollectionAlertRecordVO> records = baseMapper.selectOverdueLevelDetailList(params);
        
        // 手动构建分页结果
        Page<CustomerCollectionAlertRecordVO> page = new Page<>(current, size, total);
        page.setRecords(records);
        
        return page;
    }

    @Override
    public List<CustomerOverdueStatisticsVO> queryCustomerOverdueStatistics(CustomerCollectionAlertRecordQueryPO queryPO) {
        // 将PO对象转换为Map参数
        Map<String, Object> params = new HashMap<>();
        params.put("customerId", queryPO.getCustomerId());
        params.put("customerName", queryPO.getCustomerName());
        params.put("startTime", queryPO.getStartTime());
        params.put("endTime", queryPO.getEndTime());
        params.put("regionId", queryPO.getRegionId());
        
        // 查询客户逾期统计数据
        return baseMapper.selectCustomerOverdueStatistics(params);
    }

    @Override
    public List<CustomerOverdueStatisticsVO> queryCustomerStatisticsByLevel(CustomerCollectionAlertRecordQueryPO queryPO) {
        // 将PO对象转换为Map参数
        Map<String, Object> params = new HashMap<>();
        params.put("customerId", queryPO.getCustomerId());
        params.put("customerName", queryPO.getCustomerName());
        params.put("startTime", queryPO.getStartTime());
        params.put("endTime", queryPO.getEndTime());
        params.put("regionId", queryPO.getRegionId());
        params.put("overdueLevel", queryPO.getOverdueLevel());
        
        // 查询有指定逾期等级订单的客户统计数据
        return baseMapper.selectCustomerStatisticsByLevel(params);
    }
}
